// composables/useKeyboardAdvanced.ts
import { onMounted, onBeforeUnmount } from 'vue';

type KeyConfig = {
  key: string;
  ctrl?: boolean;
  shift?: boolean;
  alt?: boolean;
  meta?: boolean;
  callback: (event: KeyboardEvent) => void;
};

export function useKeyboardAdvanced(config: KeyConfig) {
  const handler = (event: KeyboardEvent) => {
    if (
      event.key === config.key &&
      event.ctrlKey === !!config.ctrl &&
      event.shiftKey === !!config.shift &&
      event.altKey === !!config.alt &&
      event.metaKey === !!config.meta
    ) {
      config.callback(event);
    }
  };

  onMounted(() => window.addEventListener('keydown', handler));
  onBeforeUnmount(() => window.removeEventListener('keydown', handler));
}


//使用方式
// <script setup lang="ts">
// import { useKeyboardAdvanced } from '@/composables/useKeyboardAdvanced';

// // 监听 Ctrl + Enter 组合键
// useKeyboardAdvanced({
//   key: 'Enter',
//   ctrl: true,
//   callback: (event) => {
//     event.preventDefault();
//     console.log('Ctrl+Enter 按下');
//     // 提交表单
//   }
// });
// </script>